package com.mdc.mansion.api.web.common.utils.login;

import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang.StringUtils;

import com.mdc.mansion.common.exception.BusinessException;
import com.mdc.mansion.common.util.AESUtils;
import com.mdc.mansion.common.util.MD5Utils;
import com.mdc.mansion.component.cache.common.util.CacheUtil;

/**<p>com.mdc.mansion.api.web.common.utils.login.LoginUtil</p>
 * <p>Title: LoginUtil</p>
 * <p>Copyright: Copyright © 2010-2014</p>
 * <p>Company: jumei</p>
 * @author xiang.liu
 * @version 1.0
 */
public class LoginUtil {
    private static final String PUBLIC_KEY = "7b4f385107301267fdfbad9c1ba912d7";
    private static final String TOKEN_SPLIT = "-&-";
    private static final String LOGIN_TICKET_PRE = "LOGIN_TICKET";
    public static LoginTicket getLoginTicketByToken(String token){
        if(StringUtils.isEmpty(token)){
            return null;
        }
        String tokenValue = "";
        try {
            tokenValue = AESUtils.decrypt(new String(Base64.decodeBase64(token)), PUBLIC_KEY);
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e);
        }
        String[] values = tokenValue.split(TOKEN_SPLIT);
        if(values.length!=3){
            throw new BusinessException("token pass error!");
        }
        String username = values[0];
        String tokenMd5Value = values[1];
        Long lastLoginTime = new Long(values[2]);
        String md5Value = getTokenMD5Value(username, lastLoginTime, PUBLIC_KEY);
        if(md5Value.equals(tokenMd5Value)){
            LoginTicket loginTicket = new LoginTicket();
            loginTicket.setUsername(username);
            loginTicket.setMd5Value(md5Value);
            loginTicket.setToken(token);
            return loginTicket;
        }
        return null;
    }
    private static String getTokenMD5Value(String name, Long lastLoginTime, String md5Key) {
        return MD5Utils.encryptWithKey(getTokenMD5Param(name, lastLoginTime), md5Key);
    }
    private static String getTokenMD5Param(String name, Long lastLoginTime) {
        return name + lastLoginTime.toString();
    }
    
    public static LoginTicket createUserLoginTicket(String username){
        Long lastLoginTime = System.currentTimeMillis();
        String md5Value = getTokenMD5Value(username, lastLoginTime, PUBLIC_KEY);
        StringBuilder tokenValue = new StringBuilder();
        tokenValue.append(username);
        tokenValue.append(TOKEN_SPLIT);
        tokenValue.append(md5Value);
        tokenValue.append(TOKEN_SPLIT);
        tokenValue.append(lastLoginTime);
        String token = "";
        try {
               token = Base64.encodeBase64String(AESUtils.encrypt(tokenValue.toString(),PUBLIC_KEY).getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(e);
        }
        LoginTicket loginTicket = new LoginTicket();
        loginTicket.setUsername(username);
        loginTicket.setMd5Value(md5Value);
        loginTicket.setToken(token);
        return loginTicket;
    }
    
    public static boolean loginTicketAvailable(LoginTicket loginTicket){
        String username = loginTicket.getUsername();
        String md5Value = loginTicket.getMd5Value();
        String oldMd5Value =  CacheUtil.hget(LOGIN_TICKET_PRE+username, username,String.class);
        if(oldMd5Value==null){
            return false;
        }
        String md5ValueString = oldMd5Value.toString();
        return md5Value.equals(md5ValueString);
    }

    public static void putLoginTicketInCache(LoginTicket loginTicket){
        String username = loginTicket.getUsername();
        // 将用户登录信息写入到缓存中
        CacheUtil.hset(LOGIN_TICKET_PRE+username, username, loginTicket.getMd5Value(), 3600 * 3);
    }

    public static String getLoginTicketInCache(String username){
        String token = CacheUtil.hget(LOGIN_TICKET_PRE + username, username, String.class);
        return token;
    }

    public static void removeTicketFromCache(LoginTicket loginTicket){
        String username = loginTicket.getUsername();
        CacheUtil.hdel(LOGIN_TICKET_PRE+username, username);
    }
}
